﻿using MediatR;
using SlaughterItem.Common.Domains.SetUpScale;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.DeleteandDisable
{
    /// <summary>
    /// 删除或修改磅秤表数据
    /// </summary>
    public class ScaleDelSisableHandler : IRequestHandler<ScaleDelDisableCommand, Result>
    {
        public ScaleDelSisableHandler(IBaseRepository<scaleContexts> scaleContextRepository)
        {
            ScaleContextRepository = scaleContextRepository;
        }

        public IBaseRepository<scaleContexts> ScaleContextRepository { get; set; }
        public async Task<Result> Handle(ScaleDelDisableCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Update };
            var list = ScaleContextRepository.GetAll().FirstOrDefault(x=>x.ScaleId==request.ScaleId);
            if (list != null)
            {
                if (request.pid == 13)
                {
                    var ScaleUpState = list.IsScaleDisable == true ? false : true;
                    list.IsScaleDisable = ScaleUpState;
                    var UPdateNum = ScaleContextRepository.Update(list);
                    if (UPdateNum > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        return result;
                    }
                    else
                    {
                        return result;
                    }
                }
                else if (request.pid == 15)
                {
                    var ScaleDelState = list.IsScaleDelete == true ? false : true;
                    list.IsScaleDelete = ScaleDelState;
                    var UPdateNum = ScaleContextRepository.Update(list);
                    if (UPdateNum > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        return result;
                    }
                    else
                    {
                        return result;
                    }
                }
                else
                {
                    return result;
                }
            }
            else
            { 
               return result;
            }
        }
    }
}
